package cn.edu.scut.airgallery.data;

import android.content.Context;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.MediaStore;

import java.io.File;
import java.util.ArrayList;

import cn.edu.scut.airgallery.util.StringUtils;

public class Album implements CursorHandler, Parcelable {

    public static final long ALL_MEDIA_ALBUM_ID = 8000;
    private String name, path;
    private long id = -1, dateModified;
    private int count = -1;
    private String coverpath;

    private boolean selected = false;


    public Album(String path, String name) {
        this.name = name;
        this.path = path;
    }

    public Album(String name, long id) {
        this.name = name;
        this.id = id;
    }

    public Album(String path, String name, long id, int count, long dateModified) {
        this(path, name);
        this.count = count;
        this.id = id;
        this.dateModified = dateModified;
    }

    public Album(String path, String name, int count, long dateModified) {
        this(path, name, -1, count, dateModified);
    }

    public Album(Cursor cur) {
        this(StringUtils.getBucketPathByImagePath(cur.getString(3)),
                cur.getString(1),
                cur.getLong(0),
                cur.getInt(2),
                cur.getLong(4));
        setCover(cur.getString(cur.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)));
    }

    private void setCover(String path) {
        coverpath = path;
    }

    public static String[] getProjection() {
        return new String[]{
                MediaStore.Files.FileColumns.PARENT,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                "count(*)",
                MediaStore.Images.Media.DATA,
                "max(" + MediaStore.Images.Media.DATE_MODIFIED + ")"
        };
    }

    @Override
    public Album handle(Cursor cur) {
        return new Album(cur);
    }

    @Deprecated
    public Album(Context context, String path, long id, String name, int count) {
        this(path, name, id, count, 0);
    }

    public static Album getEmptyAlbum() {
        Album album = new Album(null, null);
        return album;
    }

    public static Album getAllMediaAlbum() {
        Album album = new Album("All Media", ALL_MEDIA_ALBUM_ID);
        return album;
    }

    static Album withPath(String path) {
        Album emptyAlbum = getEmptyAlbum();
        emptyAlbum.path = path;
        return emptyAlbum;
    }

    //region Album Properties Getters
    public String getName() {
        return name;
    }

    public String getPath() {
        return path;
    }

    public int getCount() {
        return count;
    }

    public Long getDateModified() {
        return dateModified;
    }

    public String getCover(){
        return coverpath;
    }

    public long getId() {
        return  this.id;
    }

    public boolean isSelected() {
        return selected;
    }

    @Override
    public String toString() {
        return "Album{" +
                "name='" + name + '\'' +
                ", path='" + path + '\'' +
                ", id=" + id +
                ", count=" + count +
                '}';
    }
    //endregion

    public ArrayList<String> getParentsFolders() {
        ArrayList<String> result = new ArrayList<>();

        File f = new File(getPath());
        while(f != null && f.canRead()) {
            result.add(f.getPath());
            f = f.getParentFile();
        }
        return result;
    }

    //region Album Properties Setters

    public void setCount(int count) {
        this.count = count;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean setSelected(boolean selected) {
        if (this.selected == selected)
            return false;
        this.selected = selected;
        return true;
    }

    public boolean toggleSelected() {
        selected = !selected;
        return selected;
    }

    //endregion

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Album) {
            return path.equals(((Album) obj).getPath());
        }
        return super.equals(obj);
    }

    private boolean found_id_album = false;

    @Deprecated
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeString(this.path);
        dest.writeLong(this.id);
        dest.writeLong(this.dateModified);
        dest.writeInt(this.count);
        dest.writeByte(this.selected ? (byte) 1 : (byte) 0);
    }

    protected Album(Parcel in) {
        this.name = in.readString();
        this.path = in.readString();
        this.id = in.readLong();
        this.dateModified = in.readLong();
        this.count = in.readInt();
        this.selected = in.readByte() != 0;
    }

    public static final Parcelable.Creator<Album> CREATOR = new Parcelable.Creator<Album>() {

        @Override
        public Album createFromParcel(Parcel source) {
            return new Album(source);
        }

        @Override
        public Album[] newArray(int size) {
            return new Album[size];
        }
    };
}
